<?php declare(strict_types=1);

namespace App\Api\Controller;

use App\Abstract\Controller;
use App\Api\Constants\ErrorCode;
use App\Api\Handler\AccountHandler;
use App\Exception\BusinessException;
use App\Helper\RespHelper;
use App\Service\FileHandler;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Upload\UploadedFile;

class HomeController extends Controller
{

    public function uploadImage(): array
    {
        $file = $this->request->file('image');
        if (!$file instanceof UploadedFile) {
            return RespHelper::error('上传失败：请提交文件');
        }
        $business = $this->request->input('business');
        if (!in_array($business, ['avatar', 'feedback', 'comment']))
            throw new BusinessException(ErrorCode::ERR_PARAMETER, '不允许的文件类型');
        return make(FileHandler::class)->receiveFile($file, $business);
    }

    public function index(): array
    {
        return RespHelper::success();
    }
}